//
//  CTGridItem.m
//  xdmall
//
//  Created by kane on 15/3/11.
//  Copyright (c) 2015年 朱建宇. All rights reserved.
//

#import "CTGridItem.h"

#define kWidth 80
#define kHeight 45

@implementation CTGridItem
@synthesize iconImageView,titleLabel,itemIndex,delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithIndex:(NSInteger)index
{
    self = [super init];
    if (self) {
        self.itemIndex = index;
        
        self.bounds = CGRectMake(0, 0, kWidth, kWidth);
        iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth/2 - kHeight/2, 5, kHeight, kHeight)];
        [self addSubview:iconImageView];
        iconImageView.clipsToBounds = YES;
        iconImageView.layer.cornerRadius = kHeight / 2;
        iconImageView.layer.masksToBounds = YES;
        
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(iconImageView.center.x - kWidth * 0.5, kHeight + iconImageView.frame.origin.y + 2, kWidth, 15)];
        titleLabel.font = [UIFont systemFontOfSize:12.0];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:titleLabel];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickHandler:)];
        [self addGestureRecognizer:tapGesture];
    }
    return self;
}

- (void)clickHandler:(UIGestureRecognizer *)gesture
{
    if ([delegate respondsToSelector:@selector(CTGridItemClickAtIndex:)]) {
        [delegate CTGridItemClickAtIndex:self.itemIndex];
    }
}

@end
